package com.quanquan.store.service;

import DTO.PageDTO;
import com.quanquan.store.dto.FloorDTO;
import com.quanquan.store.entity.Floor;
import com.quanquan.store.entity.Store;
import com.quanquan.store.repository.FloorRepository;
import com.quanquan.store.repository.StoreRepository;
import com.quanquan.store.request.FloorRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import request.PageRequestBody;
import utils.DateUtils;
import utils.UUIDGenerator;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName:FloorService
 * @Description:FloorService
 * @Author:xiongjunbao
 * @Date:2019/3/13 9:57
 * @Version 1.0
 */
@Service
public class FloorService {

    private final FloorRepository floorRepository;
    private final StoreRepository storeRepository;

    @Autowired
    public FloorService(FloorRepository floorRepository, StoreRepository storeRepository) {
        this.floorRepository = floorRepository;
        this.storeRepository = storeRepository;
    }

    /**
     * 新增楼层信息
     * @param request
     * @return
     */
    public FloorDTO saveFloor(FloorRequest request){
        Date date = new Date();
        Floor floor = new Floor();
        floor.setId(UUIDGenerator.getUUID());
        floor.setCreateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        floor.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        floor.setSortOrder(request.getSortOrder());
        floor.setStatusRemove(0);
        floor.setStatusShow(request.getStatusShow());
//        floor.setFloor(request.getFloor());
        floor.setFloorName(request.getFloorName());
        floorRepository.saveAndFlush(floor);

        FloorDTO floorDTO = new FloorDTO();
        BeanUtils.copyProperties(floor, floorDTO);
        return floorDTO;
    }

    /**
     * 查找有效可显示的楼层信息
     * @param page
     * @param size
     * @return
     */
    public List<FloorDTO> queryList(Integer page , Integer size){
        List<FloorDTO> floorDTOList = new LinkedList<>();
        List<Floor> floorList = null;
        Sort sort = new Sort(Sort.Direction.ASC, "sortOrder");
        Pageable pageable = PageRequest.of(page, size, sort);
        Floor floor = new Floor();
        //设置floor的参数
        floor.setStatusShow(0);
        floor.setStatusRemove(0);
        Example<Floor> example = Example.of(floor);
        Page<Floor> findAll = floorRepository.findAll(example, pageable);
        floorList = findAll.getContent();
        for (Floor localFloor :floorList){
            FloorDTO floorDTO = new FloorDTO();
            BeanUtils.copyProperties(localFloor,floorDTO);
            floorDTOList.add(floorDTO);
        }
        return floorDTOList;
    }

    /**
     * 删除楼层（软删除）
     *
     * @param id
     * @return
     */
    public Integer removeFloor(String id) {
        Date date = new Date();
        Floor floor = floorRepository.findByFloorId(id);
        floor.setStatusRemove(1);
        floor.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        floorRepository.saveAndFlush(floor);
        return floor.getStatusRemove();
    }


    /**
     * 修改楼层
     *
     * @param request
     * @return
     */
    public boolean updateFloor(FloorRequest request) {
        Date date = new Date();
        Floor floor = floorRepository.findByFloorId(request.getId());
        if (floor==null){
            return false;
        }
        List<Store> storeList = storeRepository.findByStoreFloor(floor.getFloorName());
        for (Store store : storeList){
            store.setStoreFloor(request.getFloorName());
            storeRepository.saveAndFlush(store);
        }
        floor.setUpdateTime(DateUtils.dateToString(date, DateUtils.TIMESTAMP));
        floor.setSortOrder(request.getSortOrder());
        floor.setStatusRemove(0);
        floor.setStatusShow(request.getStatusShow());
//        floor.setFloor(request.getFloor());
        floor.setFloorName(request.getFloorName());
        floorRepository.saveAndFlush(floor);
        return true;
    }


    public PageDTO<Floor> allFloorForManagement(PageRequestBody requestBody) {
        Integer size = requestBody.getSize();
        Integer page = requestBody.getPage();
        Sort sort = new Sort(Sort.Direction.ASC, "sortOrder");
        Pageable pageable = PageRequest.of(page, size, sort);
        Floor floor = new Floor();
        //设置floor的参数
        floor.setStatusRemove(0);
        Example<Floor> example = Example.of(floor);
        Page<Floor> findAll = floorRepository.findAll(example, pageable);
        PageDTO<Floor> pageDTO = new PageDTO<>();
        pageDTO.setTotal(findAll.getTotalElements());
        pageDTO.setList(findAll.getContent());
        pageDTO.setTotalPage(findAll.getTotalPages());
        pageDTO.setPage(page);
        pageDTO.setPageSize(size);
        return pageDTO;
    }

    /**
     * 根据楼层id查看楼层信息
     * @param request
     * @return
     */
    public FloorDTO query(FloorRequest request) {
        Floor floor = floorRepository.findByFloorId(request.getId());

        FloorDTO floorDTO = new FloorDTO();
        //通过映射关系来赋值 //TODO 断点看一下
        BeanUtils.copyProperties(floor, floorDTO);
        return floorDTO;
    }


    public List<FloorDTO> list() {
        List<FloorDTO> floorDTOList = new LinkedList<>();
        List<Floor> floors = floorRepository.list();
        for (Floor floor : floors) {
            FloorDTO floorDTO = new FloorDTO();
            BeanUtils.copyProperties(floor, floorDTO);
            floorDTOList.add(floorDTO);
        }
        return floorDTOList;
    }

}
